import os
def upload_files_in_folder(folder_path):
    subdirs = []
    for dir in os.listdir(folder_path):
        subdirs.append(dir)
    # 递归获取目录和子目录中所有文件的列表。
    
    result = {}
    for root, dir_names, filenames in os.walk(folder_path):
        # 检查当前目录是否为二级目录
        dirs_in_path =root.split(os.sep)
        # 找出路径中实际包含的指定目录名
        contained_dirs = [dir_name for dir_name in subdirs if dir_name in dirs_in_path]
        if bool(len(contained_dirs)) and len(dirs_in_path) > len(folder_path.split(os.sep)) + 1:
            subdir_name = contained_dirs[0]
            result.update({key: subdir_name for key in filenames})
    print(result)


if __name__ == "__main__":
    target_folder = r"C:\Users\jacki\Downloads\tempDir\A案例合集\合同能源管理项目案例集（2021-2022）"
    # target_folder = "C:/Users/jacki/Downloads/tempDir/A案例合集/合同能源管理项目案例集（2021-2022）/"
    upload_files_in_folder(target_folder)